package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DecimalCalc;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PlatformFunctionFeeAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.lnfinityGradeCostAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @Author deng.gang
 * @Date 2021/7/21 下午3:23
 */
@Service
@AttrHandler(PlatformFunctionFeeAttrHandler.class)
public class PlatformFunctionFeeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if ("sceneTempl".equals(data.getExtAttributes().get("entry")) || (KiteMapUtils.isNotEmpty(extParams) && extParams.containsKey("nodeCode"))) {
            return;
        }
        //变更业务的场景初始化过程中不校验
        if (ServiceOfferConsts.SERVICE_OFFER_MODIFY.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE))
            && !"workOrder".equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), "initSource"))) {
            return;
        }
        if (!KiteObjectUtils.isEmpty(attr) && KiteStringUtils.isNotEmpty(attr.getValue())) {
            // 这里从新计算一遍最终订购费，以及重新计算折扣率
            List<KiteProdInst> prodInsts = querySceneInstService.listProdInsts(data.getSceneInstId());
            if(KiteListUtils.isEmpty(prodInsts)){
                return;
            }
            for (KiteProdInst prodInst : prodInsts) {
                List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsBySceneInstId(prodInst.getProdInstId(),data.getSceneInstId());
                for (KiteProdInstFunc func : funcs) {
                    KiteAttr finalAttr = func.getAttr("200003715"); // 最终订购费
                    if (Objects.nonNull(finalAttr) && KiteStringUtils.isNotEmpty(finalAttr.getValue())) {
                        /*
                         * 如果平台功能费存在先前值，并且当前值为空，则需要回退价格
                         * 1. 如果先前值不为空，则用先前值还原为最初的最终订购费在进行当前值的计算
                         * 2. 如果先前值为空，则用当前值进行计算(平台功能费一开始为0)
                         */
                        Double calcPrice = Double.valueOf(finalAttr.getValue());
                        if (KiteStringUtils.isNotEmpty(attr.getPreValue())) {
                            Double servicePrice = DecimalCalc.div(Double.valueOf(attr.getPreValue()), 100D);
                            calcPrice = DecimalCalc.div(calcPrice, (1 + servicePrice));
                        }
                        if (!KeyConsts.ZERO.equals(attr.getValue())) {
                            Double servicePrice = DecimalCalc.div(Double.valueOf(attr.getValue()), 100D);
                            calcPrice = DecimalCalc.mul(calcPrice, (1 + servicePrice));
                        }
                        String finalPrice = String.format("%.2f", calcPrice);
                        finalAttr.setValue(finalPrice);
                        finalAttr.setValueDesc(finalPrice);
                        operateSceneInstService.setAttr(data.getSceneInstId(), func.getInstType(), func.getInstId(), finalAttr);
                        // 重新计算一次折扣率
                        SpringUtil.getOrRegisterBean(lnfinityGradeCostAttrHandler.class).calculate(finalAttr, func, finalPrice, data);
                    }
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
